<template>
  <div class="mod-live-liveRoom">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @search-reset="searchReset"
      @refresh-change="refreshChange"
    >
      <template slot="search">
        <el-col :md="10" :xs="24">
          <el-form-item :label="this.$i18n.t('liveRoom.liveBroadcastTime')" label-width="130px">
            <el-date-picker
              size="small"
              v-model="dateRange"
              type="datetimerange"
              range-separator="—"
              value-format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="this.$i18n.t('date.start')"
              :end-placeholder="this.$i18n.t('date.end')"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </template>
      <!-- 
      <template slot-scope="scope" slot="menuLeft">
        <el-button
          v-if="isAuth('live:liveRoom:save')"
          icon="el-icon-plus"
          type="primary"
          size="small"
          @click="addOrUpdateHandle()"
        >新增</el-button>
      </template>-->
      <!-- 
        <el-button
          v-if="isAuth('live:liveRoom:update')"
          icon="el-icon-edit"
          size="small"
          type="primary"
          @click="addOrUpdateHandle(scope.row.id)"
      >修改</el-button>-->
      <template slot-scope="scope" slot="liveStatus">
        <el-tag
          v-if="scope.row.liveStatus === 101"
          size="small"
          type="danger"
        >{{$t('liveRoom.live')}}</el-tag>
        <el-tag v-if="scope.row.liveStatus === 102" size="small">{{$t('groups.hasNotStarted')}}</el-tag>
        <el-tag v-if="scope.row.liveStatus === 103" size="small">{{$t('groups.over')}}</el-tag>
        <el-tag v-if="scope.row.liveStatus === 104" size="small">{{$t('liveRoom.prohibitPlay')}}</el-tag>
        <el-tag v-if="scope.row.liveStatus === 105" size="small">{{$t('liveRoom.suspend')}}</el-tag>
        <el-tag v-if="scope.row.liveStatus === 106" size="small">{{$t('liveRoom.abnormal')}}</el-tag>
        <el-tag v-if="scope.row.liveStatus === 107" size="small">{{$t('liveRoom.expired')}}</el-tag>
      </template>
      <template slot-scope="scope" slot="roomTop">
        <el-tag v-if="scope.row.roomTop === 0" size="small">{{$t('liveRoom.notPinned')}}</el-tag>
        <el-tag
          v-else-if="scope.row.roomTop === 1"
          size="small"
          type="success"
        >{{$t('liveRoom.pinned')}}</el-tag>
      </template>
      <template slot-scope="scope" slot="menu">
        <!-- <el-button
          icon="el-icon-edit"
          size="small"
          type="text"
          :disabled="scope.row.liveStatus !== 103 "
          @click="importProd(scope.row.id)"
        >查看回放</el-button>-->
        <el-button
          type="text"
          icon="el-icon-delete"
          size="small"
          v-if="scope.row.roomTop === 0"
          :disabled="scope.row.liveStatus !== 101 && scope.row.liveStatus !== 102 "
          @click="toTopHandle(scope.row)"
        >{{$t('liveRoom.top')}}</el-button>
        <el-button
          type="text"
          icon="el-icon-delete"
          size="small"
          v-if="scope.row.roomTop === 1"
          @click="toTopHandle(scope.row)"
        >{{$t('liveRoom.cancelTopping')}}</el-button>
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
import { tableOption } from '@/crud/live/liveRoom'
import AddOrUpdate from './liveRoom-add-or-update'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {}, // 搜索
      // 时间区间
      dateRange: [],
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/live/liveRoom/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize,
              'startTime': this.dateRange === null ? null : this.dateRange[0], // 开始时间
              'endTime': this.dateRange === null ? null : this.dateRange[1] // 结束时间
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    toTopHandle (row) {
      var str = this.$i18n.t('liveRoom.top')
      if (row.roomTop) {
        str = this.$i18n.t('liveRoom.cancelTopping')
      }
      this.$confirm(this.$i18n.t('remindPop.makeSure') + ' ' + str + ' ' + this.$i18n.t('remindPop.operation') + '?', this.$i18n.t('remindPop.remind'), {
        confirmButtonText: this.$i18n.t('remindPop.confirm'),
        cancelButtonText: this.$i18n.t('remindPop.cancel'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/live/liveRoom/toTop/' + row.id),
          method: 'put',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('remindPop.succeeded'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    /**
     * 刷新回调
     */
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    // 清空自定义数据
    searchReset () {
      this.dateRange = []
    },
    searchChange (params) {
      this.searchForm = params
      this.page.currentPage = 1
      this.getDataList(this.page)
    }
  }
}
</script>
<style lang="scss">
.mod-live-liveRoom {
}
</style>
